import Vue from 'vue'
import VueRouter from 'vue-router'
import Welcome from '../views/Welcome.vue'
import store from '../store'

Vue.use(VueRouter)

//临时解决控制台报错因路由拦截产生的Uncaught(in promise) undefined
// const routerReplace=VueRouter.prototype.replace;
// VueRouter.prototype.replace= function replace(location){
//   return routerReplace.call(this,location).catch(error=>error)
// }

const routes = [
  {
    path: '/',
    name: 'Welcome',
    component: Welcome,
    meta:{
      title:"欢迎"
    }
  },
  {
    path: '/home',
    name: 'home',
    component:()=>import('@/views/Home.vue'),
    meta:{
      title:"首页"
    }
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '@/views/About.vue'),
    meta:{
      title:"关于"
    }
  },
  {
    path: '/profile',
    name: 'profile',
    component: () => import(/* webpackChunkName: "about" */ '@/views/Profile.vue'),
    meta:{
      title:"个人中心",
      requireAuth:true
    }
  },
  {
    path: '/writeBlog',
    name: 'writeBlog',
    component: () => import(/* webpackChunkName: "about" */ '@/views/WriteBlog.vue'),
    meta:{
      title:"写博客",
      requireAuth:true
    }
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '@/views/Login.vue'),
    meta:{
      title:"登录"
    }
  },
  {
    path: '/ArticleDetail',
    name: 'ArticleDetail',
    component: () => import(/* webpackChunkName: "about" */ '@/views/ArticleDetail.vue'),
    meta:{
      title:"文章详情"
    }
  },
  {
    path:'/ArticleListByTag',
    name:'ArticleListByTag',
    component:()=>import('@/views/ArticleListByTag.vue'),
    meta:{
      title:"文章列表"
    }
  },
  {
    path:'/EditMyArticle',
    name:"EditMyArticle",
    component:()=>import('@/views/EditMyArticle.vue'),
    meta:{
      title:"更新我的文章",
      requireAuth:true
    }
  },
  {
    path:'/FindPassword',
    name:"FindPassword",
    component:()=>import('@/views/FindPassword.vue'),
    meta:{
      title:"找回密码"
    }
  },
  {
    path:'/PersonalSpace',
    name:"PersonalSpace",
    component:()=>import('@/views/PersonalSpace.vue'),
    meta:{
      title:"个人空间"
    }
  },
  {
    path:'/SearchResult',
    name:"SearchResult",
    component:()=>import('@/views/SearchResult.vue'),
    meta:{
      title:"搜索结果"
    }
  }
]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
//全局路由守卫
router.beforeEach((to,from,next)=>{
  // console.log(to);
  //改下对应页面标题
  document.title=to.meta.title;
  //这里全局路由拦截判断是否登录
  if(to.meta.requireAuth){
    if(store.state.isLogin){
      next();
    }else{
      next({
        name: "login",
        replace:true
      })
    }
  }else{
    next();
  }
})
export default router
